﻿using System.Diagnostics.CodeAnalysis;
using System.Xml.Linq;

namespace _4
{
    internal class Program
    {
        public class Node<T>
        {
            public Node<T> ? Next { get; set; }
            public T Data { get; set; }
            public Node(T t)
            {
                Next = null;
                Data = t;
            }
        }
        public class GenericList<T>
        {
            private Node<T> ? head;
            private Node<T> ? tail;
            private static int sum;
            private static Node<int> ? MaxNode;
            private static Node<int> ? MinNode;
            public GenericList()
            {
                tail = head = null;
                Node<int> m = new Node<int>(int.MaxValue-1);
                Node<int> n = new Node<int>(int.MinValue);
                MaxNode = n;
                MinNode = m;
            }
            public Node<T> Head
            {
                get => head;
            }
            public Node<int> Max
            {
                get => MaxNode;
            }
            public Node<int> Min
            {
                get => MinNode;
            }
            public int Sum
            {
                get => sum;
            }
            public void Add(T t)
            {
                Node<T> n = new Node<T>(t);
                if (tail == null)
                {
                    head = tail = n;
                }
                else
                {
                    tail.Next = n;
                    tail = n;
                }
            }
            public Action<Node<int>> action1 = (x => { Console.WriteLine(x.Data); });
            public Action<Node<int>> action2 = (x => {  sum += x.Data; });
            public Action<Node<int>> action3 = (x => MaxNode=x.Data >= MaxNode .Data ? x:MaxNode );
            public Action<Node<int>> action4 = (x => MinNode = x.Data < MinNode.Data ? x:MinNode);
            public Action<int> action5 = (x => { Console.WriteLine(x); });

            public void ForEach(Action<Node<T>> action, Node<T> n )
            {
                
                while (n != null)
                {
                    action(n);
                    n = n.Next;
                }
            }
        }
        static void Main(string[] args)
        {
            GenericList<int> intlist = new GenericList<int>();
            for(int x = 0; x < 10; x++)
            {
                intlist.Add(x);
            }

            for(Node<int> node=intlist.Head;node!=null;node=node.Next )
            {
                Console.WriteLine(node.Data);
            }

            intlist.ForEach(intlist.action2, intlist.Head);

            intlist.ForEach(intlist.action3, intlist.Head);
            intlist.action1(intlist.Max);

            intlist.ForEach(intlist.action4, intlist.Head);
            intlist.action1(intlist.Min);

            intlist.action5(intlist.Sum);
        }
    }
}
